package net.learn2develop.SMSMessaging;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

//import com.jmango.x1.PBKDF2;

public class AESCrypto {

	
	private static final String DEFAULT_KEY_STRING_PREFIX = "jm_";
	
	private String softID = "13";

	private static final String DEFAULT_KEY_STRING_SUFFIX = "_encrypt";
	
	private Cipher cipher;
	
	private SecretKeySpec key;
	
	byte[] iv = new byte[] { 
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
			,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	
	public AESCrypto()
	{
		try
		{
			Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
			cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
			byte[] keyBytes = PBKDF2.deriveKey("Hashkey".getBytes(), iv, 100, 16);
			System.out.println("Hash Key:"+ asHex(keyBytes));
			key = new SecretKeySpec(keyBytes, "AES");
		}
		catch(Exception ex)
		{
			//Log..error("Error in creating AES Encryption module");
		}
	}
	
	public byte[] Decrypt(int dataLen, byte[]  encryptedData, String keyStr) throws Exception
	{
		keyStr = DEFAULT_KEY_STRING_PREFIX+softID+DEFAULT_KEY_STRING_SUFFIX;
		byte[] keyBytes = PBKDF2.deriveKey(keyStr.getBytes(), iv, 100, 16);
		key = new SecretKeySpec(keyBytes, "AES");
		// decryption pass
	    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
	    byte[] plainText = new byte[cipher.getOutputSize(dataLen)];
	    int ptLength = cipher.update(encryptedData, 0, dataLen, plainText, 0);
	    ptLength += cipher.doFinal(plainText, ptLength);
	    byte[] result = new byte[ptLength];
	    for (int i=0; i<ptLength; i++)
	    {
	    	result[i] = plainText[i];
	    }
//	    System.out.println(new String(plainText));
//	    System.out.println(ptLength);
//	    System.out.println(new String(result));
	    return result;
	}
	
	public byte[] Encrypt(byte[] input,String keyStr)throws Exception
	{
		keyStr = DEFAULT_KEY_STRING_PREFIX+softID+DEFAULT_KEY_STRING_SUFFIX;
		byte[] keyBytes = PBKDF2.deriveKey(keyStr.getBytes(), iv, 100, 16);
		key = new SecretKeySpec(keyBytes, "AES");
		// encryption pass
	    //cipher.init(Cipher.ENCRYPT_MODE, key);
	    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
	    //byte[] input = "Hello world".getBytes();
	    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
	    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
	    ctLength += cipher.doFinal(cipherText, ctLength);
//	    System.out.println(new String(cipherText));
//	    System.out.println(asHex(cipherText));
//	    System.out.println(ctLength);
	    return cipherText;
	}
	
	public static String asHex(byte buf[]) {
		StringBuffer strbuf = new StringBuffer(buf.length * 2);
		int i;

		for (i = 0; i < buf.length; i++) {
			if (((int) buf[i] & 0xff) < 0x10)
				strbuf.append("0");

			strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
		}

		return strbuf.toString();
	}
}
